﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySite.Model;
using MySite.DAL;

namespace MySite.BLL
{
    public class Blog
    {
        #region 更新微博信息
        /// <summary>
        /// 更新微博信息(插入和更新)
        /// </summary>
        /// <param name="sinfo"></param>
        /// <returns></returns>
        public ReturnMessage UpdateBlog(BlogInfo sinfo)
        {
            ReturnMessage rm = new ReturnMessage(true);
            BlogDAL sdal = new BlogDAL();
            rm = sdal.Exist(sinfo);
            if (rm.IsSuccessed)
            {
                sinfo.PID = Convert.ToInt32(rm.ReturnData["PID"]);
                rm = sdal.Update(sinfo);
            }
            else
            {
                rm = sdal.Add(sinfo);
            }

            return rm;
        }
        #endregion

        #region 更新微博信息
        /// <summary>
        /// 更新微博信息
        /// </summary>
        /// <param name="siList"></param>
        /// <returns></returns>
        public ReturnMessage UpdateBlog(IList<BlogInfo> siList)
        {
            ReturnMessage rm = new ReturnMessage(true);

            try
            {
                foreach (var item in siList)
                {
                    rm = UpdateBlog(item);

                    if (!rm.IsSuccessed)
                    {
                        return rm;
                    }
                }
            }
            catch (Exception e)
            {
                rm.IsSuccessed = false;
                rm.Exception = e;
            }

            return rm;
        }
        #endregion

        #region 查询
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<BlogInfo> Query(string where)
        {
            return new BlogDAL().Query(where);
        }

        public IList<BlogInfo> Query(string where, int top)
        {
            return new BlogDAL().Query(where, top);
        }

        public IList<BlogInfo> Query(string where, int top,string orderBy,ref int pageCount)
        {
            return new BlogDAL().Query(where, top,1,orderBy, ref pageCount);
        }
        #endregion

        public ReturnMessage Delete(string where)
        {
            return new BlogDAL().Delete(where);
        }
    }
}
